package com.example.brazil_project.function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.example.brazil_project.DucumentActivity;
import com.example.brazil_project.IndexActivity;
import com.example.brazil_project.MainActivity;
import com.example.brazil_project.parameter.Parameter;
import com.example.brazil_project.util.HttpUtil;
import com.example.brazil_project.util.IOUtil;
import com.example.brazil_project.util.ImageCutUtil;
import com.example.brazil_project.util.JSFunction;
import com.example.brazil_project.util.ParameterUtil;
import com.example.brazil_project.util.SharedPreferencesUtil;
import com.example.brazil_project.util.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.WebView;

public class LoginFunction {
	Activity mContxt; 
	Object view; 
	 public LoginFunction(Activity mContxt,Object view) {  
	        this.mContxt = mContxt;  
	        this.view = view;
	       
	  }  
	
	 /** 登录**/
	 public String sign_in(String username ,String password){
		 WebView webview = (WebView) view;
		 String msg="请输入账号密码";
		 List list = new ArrayList();
		 list.add("username");
		 list.add("password");
		 
		 //没有传密码进来
		 if(password==null||password.equals("")){
			 Map <String,String> map =  SharedPreferencesUtil.getSharedPreferencesData(mContxt, "u_p", list) ;
			//如果有保存账号密码
			 if(map==null || map.get("password")!=null){
				 username = map.get("username");
				 password = map.get("password");
			 }
		 }
		 
		 if(password!=null&&!password.equals("")){
			 //发送账号密码去验证
			 List<NameValuePair> params = new ArrayList<NameValuePair>(); 
			 params.add(new BasicNameValuePair("userName", username)); 
			 params.add(new BasicNameValuePair("password", password));
			 String url = Parameter.getServerPath()+"APP/user/SignIn.nut";
			
		 	 msg = HttpUtil.doPost(url,params);
		 }
		 
		
		 
		 //如果是密码错误则清空密码
		 if(msg!=null&&(msg.equals("密码错误")||msg.equals("用户名错误"))){
			 Map map1 = new HashMap();
			 map1.put("password", null);
			 SharedPreferencesUtil.setSharedPreferencesData(mContxt, "u_p", map1);
			
		 }
		 
		 //登录成功
		 if(StringUtils.isBlank(msg)){
			 Map map1 = new HashMap();
			 map1.put("password", password);
			 map1.put("username", username);
			 SharedPreferencesUtil.setSharedPreferencesData(mContxt, "u_p", map1);
			 Intent intent = new Intent(mContxt, IndexActivity.class);
			 //调用获取个人信息的方法设置个人角色
			 Public_method p = new Public_method(mContxt);
			 p.getUserProfileByUserName(username);
			//重启
			
			mContxt.finish();
			mContxt.startActivity(intent);	
		 }
		 webview.loadUrl(JSFunction.getAlertMSGFunction(msg)); 	
		 return msg;
	 }
	 
	 
	 /** 注册**/
	 public String login(String data){
		// String url = Parameter.getServerPath()+"APP/user/register.nut?"+data;
		 String url = Parameter.getServerPath()+"APP/user/register.nut";
		 Map map = ParameterUtil.getParameter(data);
		 String img_name = (String) map.get("img_name");
		/* List<NameValuePair> params = new ArrayList<NameValuePair>();*/
		 
		 if(!StringUtils.isBlank(img_name)){
			// ImageCutUtil.scale(img_name, result, scale, flag);
			 String FiletoString =IOUtil.FiletoString(img_name,true);
			 map.put("img_Stream", FiletoString);
			 //params.add(new BasicNameValuePair("img_Stream",FiletoString));  
		 }
		  
    	
		 Log.e("url", url);
		 String msg = HttpUtil.doPost(url,map);
		 WebView webview = (WebView) view;
		 if(msg!=null&&msg.equals("注册成功")){
			 msg = "注册成功"; 
		 }
		 webview.loadUrl(JSFunction.getAlertMSGFunction(msg)); 		 
		 return msg; 
	 }
	 
	 /**
	  * 关注用户
	  * @param username 被关注的用户名
	  */
	 public void attention(String username){
		 Public_method p = new Public_method(mContxt);
		 String my_username = p.getLoginUserName();
		 Map map = new HashMap();
		 map.put("me", my_username);
		 map.put("they", username);
		 String url = Parameter.getServerPath()+"APP/user/attention.nut";
		 String msg = HttpUtil.doPost(url,map);
		//重启
		Intent intent = mContxt.getIntent();
		mContxt.finish();
		mContxt.startActivity(intent);
	 }
	 
	 /**
	  * 取消关注用户
	  * @param username 被关注的用户名
	  */
	 public void cancelattention(String username){
		 Public_method p = new Public_method(mContxt);
		 String my_username = p.getLoginUserName();
		 Map map = new HashMap();
		 map.put("me", my_username);
		 map.put("they", username);
		 String url = Parameter.getServerPath()+"APP/user/cancelAttention.nut";
		 String msg = HttpUtil.doPost(url,map);
		//重启
		Intent intent = mContxt.getIntent();
		mContxt.finish();
		mContxt.startActivity(intent);
	 }
	
	
}
